import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**商店物品属性枚举*/
export const enum E_ShopItem {
	/**id*/
	CONFIG_ID = 0,
	/**装备等级*/
	EQUIP_LEVEL = 1,
	/**购买次数*/
	BUY_TIMES = 2,
	/**枚举数量*/
	EnumCount = 3
}


/**商店物品*/
export class ShopItem {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_SHOP_ITEM_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_SHOP_ITEM_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: ShopItem[] = [];

	/**创建入口*/
	public static create(json?: string): ShopItem {
		const c = this.POOL.pop() || new ShopItem();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**id (uint32)*/
	protected _configId: number;
	/**装备等级 (uint16)*/
	protected _equipLevel: number;
	/**购买次数 (uint16)*/
	protected _buyTimes: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (ShopItem.POOL.indexOf(this) === -1) {
			ShopItem.POOL.push(this);
		}
	}

	/**id*/
	public get configId() {
		return this._configId;
	}


	/**id*/
	public set configId(v: number) {
		this._configId = v;
	}


	/**装备等级*/
	public get equipLevel() {
		return this._equipLevel;
	}


	/**装备等级*/
	public set equipLevel(v: number) {
		this._equipLevel = v;
	}


	/**购买次数*/
	public get buyTimes() {
		return this._buyTimes;
	}


	/**购买次数*/
	public set buyTimes(v: number) {
		this._buyTimes = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_ShopItem): any {
		switch (k) {
			case E_ShopItem.CONFIG_ID: return this._configId;
			case E_ShopItem.EQUIP_LEVEL: return this._equipLevel;
			case E_ShopItem.BUY_TIMES: return this._buyTimes;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_ShopItem, v: any): void {
		switch (k) {
			case E_ShopItem.CONFIG_ID: this._configId = v; break;
			case E_ShopItem.EQUIP_LEVEL: this._equipLevel = v; break;
			case E_ShopItem.BUY_TIMES: this._buyTimes = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//id
		this._configId = 0;
		//装备等级
		this._equipLevel = 0;
		//购买次数
		this._buyTimes = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._configId) {
			json.configId = this._configId;
		}
		if (this._equipLevel) {
			json.equipLevel = this._equipLevel;
		}
		if (this._buyTimes) {
			json.buyTimes = this._buyTimes;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.configId) {
			this._configId = json.configId;
		}
		if (json.equipLevel) {
			this._equipLevel = json.equipLevel;
		}
		if (json.buyTimes) {
			this._buyTimes = json.buyTimes;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeUint32(this._configId);
		byte.writeUint16(this._equipLevel);
		byte.writeUint16(this._buyTimes);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._configId = byte.readUint32();
		this._equipLevel = byte.readUint16();
		this._buyTimes = byte.readUint16();
		return this;
	}


	/**克隆*/
	public clone(): ShopItem {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = ShopItem.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = ShopItem.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//id
			if (idx === E_ShopItem.CONFIG_ID) {
				const oldv = this._configId;
				this._configId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._configId, oldv);
				i += 2;
				continue;
			}
			//装备等级
			if (idx === E_ShopItem.EQUIP_LEVEL) {
				const oldv = this._equipLevel;
				this._equipLevel = us[i + 1];
				IAsync.dispatch(eventID, idx, this._equipLevel, oldv);
				i += 2;
				continue;
			}
			//购买次数
			if (idx === E_ShopItem.BUY_TIMES) {
				const oldv = this._buyTimes;
				this._buyTimes = us[i + 1];
				IAsync.dispatch(eventID, idx, this._buyTimes, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in ShopItem with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: ShopItem): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._configId !== other._configId) {
			updates.push(E_ShopItem.CONFIG_ID, this._configId);
		}
		if (this._equipLevel !== other._equipLevel) {
			updates.push(E_ShopItem.EQUIP_LEVEL, this._equipLevel);
		}
		if (this._buyTimes !== other._buyTimes) {
			updates.push(E_ShopItem.BUY_TIMES, this._buyTimes);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(ShopItem.TOTAL_EVENT_ID, this.getV(0));
	}

}
